﻿package net.libra.utils
{
	import flash.display.Stage;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;


	/**
	 * 常用方法
	 * @author DanielLu
	 */
	public class WarUtils 
	{
		/**
		 * 将秒数转换成时间，格式为00:00:00
		 * @return		时间字符串
		 */
		public static function formatTime (time : Number) : String
		{
			if(time < 0 || isNaN( time ))
			{
				return "--:--:--";
			}
			var h : Number = Math.floor( time / 3600 );
			var m : Number = Math.floor( Number( time - 3600 * h ) / 60 );
			var s : Number = Math.floor( time - 3600 * h - 60 * m );
			var hh : String,mm : String,ss : String;
			if (h < 10)
			{
				hh = "0" + h;
			}
			else
			{
				hh = String( h );
			}
			if (m < 10)
			{
				mm = "0" + m;
			}
			else
			{
				mm = String( m );
			}
			if (s < 10)
			{
				ss = "0" + s;
			}
			else
			{
				ss = String( s );
			}
			var currentTime : String = hh + ":" + mm + ":" + ss;
			return currentTime;
		}

		/**
		 * 调用web页面
		 * @param url		调用的web页面地址
		 */
		public static function openHelpURL (url : String) : void
		{
			
		}

		/**
		 * 调用web页面
		 * @param url		调用的web页面地址
		 */
		public static function getURL (url : String,...arg,stage:Stage) : void
		{
			if (ExternalInterface.available)
			{
				try
				{
					var x : Number = stage.stageWidth / 2;
					var y : Number = stage.stageHeight / 2;
					ExternalInterface.call.apply( null , [ "setIFrameContent", x,y, url ].concat( arg ) );
					//ExternalInterface.call( "setIFrameContent" , url );
					//显示黑屏
					BlackPopup.getInstance( ).open( );
				}
				catch (error : SecurityError)
				{
					trace( error.message );
				}
				catch (error : Error)
				{
					trace( error.message );
				}
			}
		}

		/**
		 * 外部连接
		 */
		public static function openURL (url : String,window : String = "_blank") : void
		{
			navigateToURL( new URLRequest( url ) , window ) ;
		}

		/**
		 * 获取随机数
		 */
		public static function getRandom (min : Number, max : Number) : Number 
		{
			var randomNum : Number = Math.floor( Math.random( ) * (max - min + 1) ) + min;
			return randomNum;
		}
	}
}

import com.cw.war.ui.AbstractPopup;
import flash.external.ExternalInterface;


class BlackPopup extends AbstractPopup
{
	private static var instance : BlackPopup;

	public static function getInstance () : BlackPopup
	{
		if(instance == null)instance = new BlackPopup( );
		return instance;
	}

	public function BlackPopup ()
	{
		var dis : BlackPopup = this;
		ExternalInterface.addCallback( "close" , function(e : Object = null):void
		{
			if(parent)parent.removeChild( dis );
			//throw new Error( "aaaa" );
			e;
		} );
	}
}
